package org.example.Customer;

import java.util.HashMap;
import java.util.Map;

public class Supermarket {
    private Map<String, Fruit> fruits = new HashMap<>();
    private int discountThreshold;
    private int discountAmount;

    public Supermarket() {
        // 初始化水果价格
        fruits.put("apple", new Fruit("apple", 8, 1.0));
        fruits.put("strawberry", new Fruit("strawberry", 13, 0.8));
        fruits.put("mango", new Fruit("mango", 20, 1.0));

        // 设置满减优惠
        this.discountThreshold = 100;
        this.discountAmount = 10;
    }

    public double calculateTotal(Map<String, Integer> fruitWeights) {
        double total = 0;

        // 计算各水果总价
        for (Map.Entry<String, Integer> entry : fruitWeights.entrySet()) {
            String fruitName = entry.getKey();
            int weight = entry.getValue();

            if (fruits.containsKey(fruitName)) {
                Fruit fruit = fruits.get(fruitName);
                total += fruit.getDiscountedPrice() * weight;
            }
        }

        // 应用满减优惠
        if (total >= discountThreshold) {
            total -= discountAmount;
        }

        return total;
    }

    public static void main(String[] args) {
        Supermarket supermarket = new Supermarket();

        // 测试用例
        Map<String, Integer> order1 = new HashMap<>();
        order1.put("apple", 2);
        order1.put("strawberry", 3);
        order1.put("mango", 1);
        System.out.println("订单1总价: " + supermarket.calculateTotal(order1)); // 预期: 76.4

        Map<String, Integer> order2 = new HashMap<>();
        order2.put("apple", 8);
        order2.put("strawberry", 4);
        order2.put("mango", 3);
        System.out.println("订单2总价: " + supermarket.calculateTotal(order2)); // 预期: 134.0
    }
}
